package com.tao.controller;

import com.tao.entity.Message;
import com.tao.entity.User;
import com.tao.service.LoginService;
import com.tao.service.MessageService;
import com.tao.utils.SecurityUtils;
import com.tao.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author  诸葛先生
 * 功能：聊天模块接口
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    /**
     * 查询与当前登录用户交流过的所有用户
     * @return
     */
    @GetMapping("userList")
    public ResponseResult getUsers() {
        Long userId = SecurityUtils.getUserId();
        //查询出跟当前登录用户有信息交流的所有用户
        List<User> friends = messageService.getFriends(userId);
        return ResponseResult.okResult(friends);
    }

    /**
     * 查询当前用户与目标用户的所有历史消息
     * @param toId
     * @return
     */
    @GetMapping("history/{toId}")
    public ResponseResult getMessages(@PathVariable Long toId) {
        Long userId = SecurityUtils.getUserId();
        List<Message> messages = messageService.getMessages(userId, toId);
        return ResponseResult.okResult(messages);
    }
}
